package thread_ex;

import java.util.Random;

class MyThread05 extends Thread{
	public void run(){
		Random rnd = new Random();
		for(int i=0;i<=10;i++){
			System.out.println(rnd.nextInt(10) + " " + getName());
		}
		System.out.println("Thread is finished");		
	}
	public MyThread05(String name) {
		super(name);
	}
}


public class Test05_Join {

	public static void main(String[] args) {
		MyThread05 mt1 = new MyThread05("1");
		MyThread05 mt2 = new MyThread05("2");
		MyThread05 mt3 = new MyThread05("3");
		
		mt2.setPriority(Thread.MAX_PRIORITY); //1
		mt1.setPriority(Thread.MIN_PRIORITY); //3 
		mt3.setPriority(Thread.NORM_PRIORITY); //2
		
		
		mt1.start();
		mt2.start();
		mt3.start();
		try{ 
			mt1.join(); // run()이 다 실행될 때까지 기다리게 한다. try-catch 를 모두 주석처리하면 메인메서드가 먼저 끝나버리는 결과를 볼 수 있다.
			mt2.join();
			mt3.join();
		}catch(InterruptedException ie){
			System.out.println(ie.getMessage());
		}
		System.out.println("Main is finished");
	}

}
